package com.david.zhufengfm.adapters;

/**
 * Created by David on 2015/10/21.
 * qq:978944820
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.david.zhufengfm.R;
import com.david.zhufengfm.entity.AlbumBasic;
import com.david.zhufengfm.entity.discoverrecommend.AlbumRecommend;
import com.david.zhufengfm.entity.discoverrecommend.ColumnsRecommend;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendAlbums;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendColumns;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendFocus;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendItem;
import com.david.zhufengfm.entity.discoverrecommend.DiscoverRecommendSpecial;
import com.david.zhufengfm.entity.discoverrecommend.FocusRecommend;
import com.david.zhufengfm.entity.discoverrecommend.SpecialRecommend;
import com.david.zhufengfm.widgets.ColumnsItemView;
import com.david.zhufengfm.widgets.SpecialItemView;
import com.squareup.picasso.Picasso;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 发现推荐部分的ListView Adapter
 * 支持多布局的复用
 */
public class DiscoverRecommendAdapter extends BaseAdapter{
    private Context context;
    private List<DiscoverRecommendItem>items;
    //--------------------------------------------
    //点击事件处理器
    /**
     * 推荐专辑点击事件
     */
    private View.OnClickListener onRecommendAlbumClickListener;
    public DiscoverRecommendAdapter(Context context, List<DiscoverRecommendItem> items) {
        this.context = context;
        this.items = items;
    }

    public void setOnRecommendAlbumClickListener(View.OnClickListener onRecommendAlbumClickListener) {
        this.onRecommendAlbumClickListener = onRecommendAlbumClickListener;
    }

    @Override
    public int getCount() {
        int count=0;
        if (items != null) {
            count=items.size();
        }
        return count;
    }

    @Override
    public int getViewTypeCount() {
        return 3;//当前的ListView显示三种类型，小编推荐/热门推荐，精品听单，发现新奇三种
    }

    @Override
    public int getItemViewType(int position) {
        int ret=0;
        DiscoverRecommendItem item=items.get(position);
        if(item instanceof DiscoverRecommendAlbums){//小编推荐，热门推荐

            ret=2;

        }else if(item instanceof DiscoverRecommendColumns) {//发现新奇
            ret=1;
        }else if(item instanceof DiscoverRecommendSpecial){//精品听单
            ret=0;
        }


        return ret;
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret=null;
        //1.获取数据
        DiscoverRecommendItem item=items.get(position);
        if(item instanceof DiscoverRecommendAlbums){//小编推荐，热门推荐

            ret=bindAlbumView(position,convertView,parent);

        }else if(item instanceof DiscoverRecommendColumns) {//发现新奇
                ret=bindColumnView(position,convertView,parent);
        }else if(item instanceof DiscoverRecommendSpecial){//精品听单
                ret=bindSpecialView(position,convertView,parent);
        }

        return ret;
    }

    /**
     * 加载专辑推荐的Item：小编推荐和热门推荐
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindAlbumView(int position,View convertView,ViewGroup parent){
        View ret=null;
        //1.复用
        if (convertView != null) {
            ret=convertView;
        }else{
            ret= LayoutInflater.from(context).
                    inflate(R.layout.discover_recommend_album_item, parent, false);
        }

        //2.ViewHolder
        AlbumViewHolder holder= (AlbumViewHolder) ret.getTag();
        if (holder == null) {
            holder=new AlbumViewHolder();
            holder.txtTitle= (TextView) ret.findViewById(R.id.recommend_album_title);
            holder.txtMore= (TextView) ret.findViewById(R.id.recommend_album_more);
           //TODO 点击more，进入推荐列表
            holder.albumIcons=new ImageView[3];
            holder.albumNames=new TextView[3];
            holder.trackNames=new TextView[3];

            for (int i = 0; i < 3; i++) {
                //给Holder设置数组的内容
               holder.albumIcons[i]=
                       (ImageView) findView(ret,"recommend_album_icon_"+i);
               //TODO 点击专辑图片，进入专辑详情

                holder.albumIcons[i].setOnClickListener(onRecommendAlbumClickListener);

                holder.albumNames[i]= (TextView) findView(ret,"recommend_album_name_"+i);
                holder.trackNames[i]= (TextView) findView(ret,"recommend_album_track_name_"+i);
            }
            ret.setTag(holder);

        }
        //3.获取数据，设置内容
        DiscoverRecommendAlbums albums= (DiscoverRecommendAlbums) items.get(position);
        String title=albums.getTitle();
        holder.txtTitle.setText(title);
        if (albums.isHasMore()){
            holder.txtMore.setVisibility(View.VISIBLE);
        }else{
            holder.txtMore.setVisibility(View.INVISIBLE);
        }
        for (int i = 0; i < 3; i++) {
            ImageView imageView = holder.albumIcons[i];
            AlbumRecommend albumRecommend = albums.getAlbumRecommends().get(i);
            imageView.setTag(albumRecommend);

            String url = albumRecommend.getCoverLarge();
            if (url != null) {
                Picasso.with(context).load(url).placeholder(R.mipmap.ic_launcher).
//                        centerCrop().resize(256,256).
                        into(imageView);
            }
            holder.albumNames[i].setText(albums.getAlbumRecommends().get(i).getTitle());
            holder.trackNames[i].setText(albums.getAlbumRecommends().get(i).getTrackTitle());

        }
        return ret;
    }
    /**
     * 加载发现新奇的Item
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindColumnView(int position,View convertView,ViewGroup parent){
        View ret=null;
        if (convertView != null) {
            ret=convertView;
        }else{
            ret=LayoutInflater.from(context).inflate(R.layout.discover_recommend_columns_item,
                    parent,false);
        }
        ColumnsViewHolder holder= (ColumnsViewHolder) ret.getTag();
        if (holder == null) {
            holder=new ColumnsViewHolder();
            holder.txtTitle= (TextView) ret.findViewById(R.id.recommend_columns_title);
            holder.txtMore= (TextView) ret.findViewById(R.id.recommend_columns_more);
            holder.linearLayout= (LinearLayout) ret.findViewById(R.id.recommend_columns_container);
            ret.setTag(holder);
        }
        // TODO 获取数据，展示数据
        DiscoverRecommendColumns recommendColumns= (DiscoverRecommendColumns) items.get(position);
        holder.txtTitle.setText(recommendColumns.getTitle());
        if (recommendColumns.isHasMore()){
            holder.txtMore.setVisibility(View.VISIBLE);
        }else{
            holder.txtMore.setVisibility(View.INVISIBLE);
        }
        holder.linearLayout.removeAllViews();
        List<ColumnsRecommend> columnsRecommends = recommendColumns.getColumnsRecommends();
        if (columnsRecommends != null) {
            int index=-1;
            int itemCount=columnsRecommends.size();
            int icons[]={R.mipmap.finding_zone_img,R.mipmap.finding_activity_img,R.mipmap.finding_mart_img,R.mipmap.find_ring_img};
            for (ColumnsRecommend columnsRecommend:columnsRecommends) {
                index++;
                //创建自定义View，用来显示听单信息
                ColumnsItemView columnsItemView=new ColumnsItemView(context);
                LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                columnsItemView.setLayoutParams(lp);
                holder.linearLayout.addView(columnsItemView);
                columnsItemView.setTitle(columnsRecommend.getTitle());
                columnsItemView.setSubTitle(columnsRecommend.getSubtitle());

                ImageView imageView=columnsItemView.getImgIcon();
                imageView.setImageResource(icons[index]);


                //判断是否是最后一个
                columnsItemView.setShowBottomLine(true);
                if (index==itemCount-1){
                    //最后一个
                    columnsItemView.setShowBottomLine(false);
                }
            }
        }
        return ret;
    }
    /**
     * 加载精品推荐的Item
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    private View bindSpecialView(int position,View convertView,ViewGroup parent){
        View ret=null;
        //1.复用
        if (convertView != null) {
            ret=convertView;
        }else{
            ret=LayoutInflater.from(context).
                    inflate(R.layout.discover_recommend_special_item,
                            parent,false);

        }
        //ViewHolder处理
        SpecialViewHolder holder= (SpecialViewHolder) ret.getTag();
        if (holder == null) {
            holder=new SpecialViewHolder();
            holder.txtTitle= (TextView) ret.findViewById(R.id.recommend_special_title);
            holder.txtMore= (TextView) ret.findViewById(R.id.recommend_special_more);
            holder.itemContainer= (LinearLayout) ret.findViewById(R.id.recommend_special_container);
            ret.setTag(holder);
        }
        //获取数据，显示数据
        DiscoverRecommendSpecial special= (DiscoverRecommendSpecial) items.get(position);
        holder.txtTitle.setText(special.getTitle());
        if (special.isHasMore()){
            holder.txtMore.setVisibility(View.VISIBLE);
        }else{
            holder.txtMore.setVisibility(View.INVISIBLE);
        }
        //3.1清空旧的LinearLayout数据，在根据听单的item来添加
        holder.itemContainer.removeAllViews();
        List<SpecialRecommend> specialRecommends = special.getSpecialRecommends();
        if (specialRecommends != null) {
            int index=-1;
            int itemCount=specialRecommends.size();
            for (SpecialRecommend specialRecommend:specialRecommends) {
                index++;
                //创建自定义View，用来显示听单信息
                SpecialItemView itemView=new SpecialItemView(context);
                LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                itemView.setLayoutParams(lp);
                holder.itemContainer.addView(itemView);
                itemView.setTitle(specialRecommend.getTitle());
                itemView.setSubTitle(specialRecommend.getSubtitle());
                itemView.setNumber(specialRecommend.getFootnote());
                ImageView imageView=itemView.getImgIcon();
                String coverPath=specialRecommend.getCoverPath();
                if (coverPath != null) {
                    Picasso.with(context).load(coverPath).into(imageView);
                }

                //判断是否是最后一个
                itemView.setShowBottomLine(true);
                if (index==itemCount-1){
                    //最后一个
                    itemView.setShowBottomLine(false);
                }
            }
        }
        return ret;
    }

    /**
     * 利用类的反射，来获取指定R.id.xxxx代表的View
     * @param container
     * @param fieldName
     * @return
     */
    public static View findView(View container,String fieldName){
        View ret=null;
        if (container != null&fieldName!=null) {
            Class<R.id> idClass = R.id.class;
            try {
                Field field = idClass.getDeclaredField(fieldName);
                int id = field.getInt(idClass);
                //通过静态常量，获取int值，来查找View
                ret=container.findViewById(id);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
    /**
     * 小编推荐的ViewHolder
     */
    private static class AlbumViewHolder{
        public TextView txtTitle;
        public TextView txtMore;
        public ImageView[]albumIcons;//三张图片在不同的RelativeLayout
        public TextView[] albumNames;//三个专辑名称
        public TextView[] trackNames;//三个曲目名称
    }

    private static class SpecialViewHolder{
        public TextView txtTitle;
        public TextView txtMore;
        //存储SpecialItemView
        public LinearLayout itemContainer;

    }
    private static class ColumnsViewHolder{
        public TextView txtTitle;
        public TextView txtMore;
        public LinearLayout linearLayout;
    }
}
